package com.kgc.javaweb.dao.impl;


import cn.hutool.json.JSONUtil;
import com.mysql.jdbc.StringUtils;
import com.kgc.javaweb.dao.BuildDao;
import com.kgc.javaweb.dto.UserPageDTO;
import com.kgc.javaweb.entity.Build;
import com.kgc.javaweb.enums.CodeMsg;
import com.kgc.javaweb.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/11/9
 */
public class BuildDaoImpl implements BuildDao {
    


    @Override//用
    public List<Build> selectPage(UserPageDTO userPageDTO) {

        Integer page = userPageDTO.getPage();
        Integer limit = userPageDTO.getLimit();

        Integer start = (page-1)*limit;


        String searchParams = userPageDTO.getSearchParams();

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());

        String sql = "select *  from build  where 1=1 ";

        ArrayList<Object> params = new ArrayList<>();

        // 搜索分页
        if(searchParams!=null){
            //  username  address
            Build build = JSONUtil.toBean(searchParams, Build.class);
            String buildname = build.getBuildname();
            String type = build.getType();

            if(!StringUtils.isEmptyOrWhitespaceOnly(buildname)){
                sql+=" and buildname = ?";
                params.add(buildname);
            }
            if(!StringUtils.isEmptyOrWhitespaceOnly(type)){
                sql+=" and type = ?";
                params.add(type);
            }
        }
        sql+=" limit ? , ?";
        System.out.println("sql = " + sql);
        params.add(start);
        params.add(limit);
        System.out.println("params = " + params);

        try {
            List<Build> builds = queryRunner.query(sql, new BeanListHandler<Build>(Build.class), params.toArray());
            return  builds;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new  RuntimeException("分页查询失败");
        }
    }

    @Override//用
    public Long selectTotal(UserPageDTO userPageDTO) {

        //查询总条数   搜索总条数
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());

        String sql = "select count(*) from build where 1=1 ";

        String searchParams = userPageDTO.getSearchParams();
        ArrayList<Object> params = new ArrayList<>();

        if(searchParams!=null){

            Build build = JSONUtil.toBean(searchParams, Build.class);
            String buildname = build.getBuildname();
            String type = build.getType();

            if(!StringUtils.isEmptyOrWhitespaceOnly(buildname)){
                sql+=" and buildname = ?";
                params.add(buildname);
            }
            if(!StringUtils.isEmptyOrWhitespaceOnly(type)){
                sql+=" and type = ?";
                params.add(type);
            }
        }

        try {
            Long total = queryRunner.query(sql, new ScalarHandler<Long>(),params.toArray());
            return  total;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException("查询总条数失败");
        }
    }

    @Override//用
    public Build selectById(Integer id) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select *  from build where id = ?";
        try {
            Build build = queryRunner.query(sql, new BeanHandler<Build>(Build.class), id);
            return build;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException("查询数据失败！");
        }
    }

    @Override//用
    public void update(Build buil,Integer id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "update build set buildname = ? ,type = ? where id = ?";
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        String strBirth = simpleDateFormat.format(buil.getBirth());

        try {
            queryRunner.update(sql, buil.getBuildname(), buil.getType(), id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException(CodeMsg.UPDATE_FAIL.getMsg());
        }

    }

    @Override//用
    public void add(Build buil) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "insert into build (buildname,type)values(?,?)";
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        String strBirth = simpleDateFormat.format(buil.getBirth());
        try {
            int update = queryRunner.update(sql, buil.getBuildname(), buil.getType());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException(CodeMsg.ADD_FAIL.getMsg());
        }
    }

    @Override//用
    public void delete(String ids) {  // 1,2,3  1

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "delete from build where id = ?";

        String[] split = ids.split(",");
        ArrayList<Integer> allId = new ArrayList<>();
        for (String s : split) {
            allId.add(Integer.valueOf(s));
        }

        allId.forEach(id->{
            try {
                queryRunner.update(sql, id);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                throw new  RuntimeException(CodeMsg.DELETE_FAIL.getMsg());
            }
        });
    }
}
